package cn.ekuma.epos.datalogic.define.dao.erp;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.datalogic.define.dao.CustomerInfoDAO;
import cn.ekuma.epos.db.table.erp.I_OperationCustomer;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.erp.OperationCustomer;
import com.openbravo.bean.erp.OperationProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class OperationCustomerDAO extends ModifiedLogDAO<OperationCustomer> {
	   CustomerInfoDAO customerInfoDAO;
		public OperationCustomerDAO(I_Session s,CustomerInfoDAO customerInfoDAO) {
			super(s);
		 this.customerInfoDAO=customerInfoDAO;
		}

		public OperationCustomerDAO(I_Session s) {
			super(s);
		}

		@Override
		public TableDefinition getTable() {
			return new TableDefinition(s, "OPERATION_CUSTOMER", new Field[] {
					new Field(I_OperationCustomer.ID, Datas.STRING, Formats.STRING),
					new Field(I_OperationCustomer.OPERATIONCATEGORIE, Datas.STRING, Formats.STRING,
							OperationProductCategory.class),
					new Field(I_OperationCustomer.CUSTOMER, Datas.STRING, Formats.STRING,
							BusinessPartner.class),
					new Field(I_OperationCustomer.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)},
					new int[] { 0 });
		}

		@Override
		public OperationCustomer readValues(DataRead dr,OperationCustomer obj) throws BasicException {
			if(obj==null)
				obj=new OperationCustomer();
			obj.setId(dr.getString(1));
			obj.setCategoryId(dr.getString(2));
			obj.setCustomerId(dr.getString(3));
			obj.setLastModified(dr.getTimestamp(4));
			if(customerInfoDAO!=null)
			  obj.setCustomer(customerInfoDAO.findCustomerInfoByID(obj.getCustomerId()));
			return obj;
		}

		@Override
		public void writeInsertValues(DataWrite dp, OperationCustomer obj)
				throws BasicException {
			dp.setString(1,obj.getId());
			dp.setString(2,obj.getCategoryId());
			dp.setString(3,obj.getCustomerId());
			dp.setTimestamp(4,obj.getLastModified());
		}

		@Override
		public Class getSuportClass() {
			return OperationCustomer.class;
		}
		

	}
